//
//  ViewController.m
//  searchBar
//
//  Created by MB__Lee on 2018/4/13.
//  Copyright © 2018年 MB__Lee. All rights reserved.
//

#import "ViewController.h"

@interface ViewController ()<UISearchBarDelegate>

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    UISearchBar *bar = [[UISearchBar alloc] initWithFrame:CGRectMake(0, 20, self.view.bounds.size.width, 200)];
    [self.view addSubview:bar];
    
    bar.delegate = self;
    bar.barStyle = UIBarStyleDefault;
    bar.barTintColor =  [UIColor blueColor];
    bar.tintColor = [UIColor greenColor];
    bar.searchBarStyle = UISearchBarStyleMinimal;
    bar.translucent = NO;
    
//    bar.placeholder  = @"doubi";
//    bar.text = @"xuting";
//    bar.prompt = @"tishi";
    
//    bar.showsCancelButton = YES;
//    bar.showsSearchResultsButton = YES;
   
    
    /*************** 只要设置了scopeButton 相关的 东西，bar尺寸会自动变成标准尺寸****************/
    
    bar.scopeButtonTitles = @[@"doubi",@"xuting",@"ermeng",@"doubi"];
    bar.showsScopeBar = YES;
    
    [bar setPositionAdjustment:UIOffsetMake(-199, -200) forSearchBarIcon:UISearchBarIconSearch];
    [bar setSearchTextPositionAdjustment:UIOffsetMake(-199, -200)];
    
//    bar.backgroundImage = [UIImage imageNamed:@"2"];
    
    
    
    [self searchbar1];
    
}

-(void)searchbar1{
    UISearchBar *bar = [[UISearchBar alloc] initWithFrame:CGRectMake(0, 400, self.view.bounds.size.width, 200)];
    bar.backgroundColor  = [UIColor grayColor];
    [self.view addSubview: bar];
    bar.delegate = self;
    
    
    
    //text content
    bar.placeholder = @"doubi";
    bar.text = @"xuting";
    bar.prompt = @"prompt"; //设置此属性，会将 输入文字的地方下移，不设置此属性，输入框自动上下居中
    
    
    /***************display attribute****************/
    
    bar.barStyle = UIBarStyleDefault;
    bar.barTintColor = [UIColor greenColor]; //设置prompt 文字的颜色
    bar.searchBarStyle = UISearchBarStyleProminent;
    bar.translucent = NO;
    //    bar.tintColor = [UIColor redColor]; // 设置光标和cancel 的颜色
    
    /***************button configuration****************/
    
    bar.showsBookmarkButton = YES; //和searchResultButton 在同一个位置显示，因此只能设置显示其中一个
    bar.showsCancelButton = YES;
    [bar setShowsCancelButton:YES animated:YES];
    //    bar.showsSearchResultsButton = YES;
    
    
    /*************** scope buttons****************/
//    bar.scopeButtonTitles = @[@"doubi",@"ermeng",@"1",@"3"];
//    bar.showsScopeBar = NO;
    
    /*************** Customizing Appearance****************/
    //    bar.backgroundImage = [UIImage imageNamed:@"backgroundImage"];
    
    //    [bar setBackgroundImage:[UIImage imageNamed:@"backgroundImage"] forBarPosition:UIBarPositionAny barMetrics:UIBarMetricsDefaultPrompt];
    
    [bar setBackgroundImage:nil forBarPosition:UIBarPositionTopAttached barMetrics:nil];
    
    [bar setImage:[UIImage imageNamed:@"1"] forSearchBarIcon:UISearchBarIconSearch state:UIControlStateNormal];
    [bar setImage:[UIImage imageNamed:@"2"] forSearchBarIcon:UISearchBarIconBookmark state:UIControlStateNormal];
    
    [bar setPositionAdjustment:UIOffsetMake(100, -9) forSearchBarIcon:UISearchBarIconSearch];
    
    UISwitch *swi = [[UISwitch alloc] init];
    swi.backgroundColor  =[UIColor greenColor];
    bar.inputAccessoryView = swi;
    
    //    [bar setScopeBarBackgroundImage:[UIImage imageNamed:@"1"]];
//    [bar setScopeBarButtonDividerImage:[UIImage imageNamed:@"3"] forLeftSegmentState:UIControlStateNormal rightSegmentState:UIControlStateNormal];
    //
    //    [bar setSearchFieldBackgroundImage:[UIImage imageNamed:@"1"] forState:UIControlStateNormal];
    [bar setSearchTextPositionAdjustment:UIOffsetMake(100, 0)];
}
    
#pragma mark ---searchBarDelegate
-(BOOL)searchBarShouldBeginEditing:(UISearchBar *)searchBar{
    return  YES;
}
-(void)searchBarTextDidBeginEditing:(UISearchBar *)searchBar{
    NSLog(@"%s",__func__);
}
-(BOOL)searchBarShouldEndEditing:(UISearchBar *)searchBar{
    return  YES;
}
-(void)searchBarTextDidEndEditing:(UISearchBar *)searchBar{
    NSLog(@"%s",__func__);
}
-(void)searchBar:(UISearchBar *)searchBar textDidChange:(NSString *)searchText{
    NSLog(@"%@",searchText);
}
-(BOOL)searchBar:(UISearchBar *)searchBar shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text{
    
    NSLog(@"range-----%@",range);
    NSLog(@"text--------%@",text);
    return  YES;
}
/******************************/
-(void)searchBarSearchButtonClicked:(UISearchBar *)searchBar{
    NSLog(@"搜索按钮点击");
}
-(void)searchBarCancelButtonClicked:(UISearchBar *)searchBar{
    NSLog(@"取消按钮点击");
}
-(void)searchBarBookmarkButtonClicked:(UISearchBar *)searchBar{
    NSLog(@"书框按钮点击");
}
-(void)searchBarResultsListButtonClicked:(UISearchBar *)searchBar{
    NSLog(@"搜索结果按钮点击");
}
-(void)searchBar:(UISearchBar *)searchBar selectedScopeButtonIndexDidChange:(NSInteger)selectedScope{
    NSLog(@"点击了第--%zd--个按钮",selectedScope);
}





- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


@end
